package com.slz3.business.module.configManagement.conSiteProvince.service;

import com.slz3.business.frame.enums.ValidFlag;
import com.slz3.business.frame.module.Repositorys;
import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.steward.PageableSteward;
import com.slz3.business.module.configManagement.conSiteProvince.domain.ConSiteProvince;
import com.slz3.business.module.configManagement.conSiteProvince.domain.ConSiteProvinceFactory;
import com.slz3.business.module.configManagement.conSiteProvince.steward.ConSiteProvinceSpecification;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;

import java.util.*;

@Slf4j
@Service
public class ConSiteProvinceServiceImpl implements ConSiteProvinceService {

    @Override
    public ConSiteProvince save(ConSiteProvince conSiteProvince) {
        return Repositorys.conSiteProvince.save(ConSiteProvinceFactory.initBaseFields(conSiteProvince));
    }

    @Override
    public Page<ConSiteProvince> page(MultiValueMap<String, String> params) {
        Page<ConSiteProvince> conSiteProvincePage = Repositorys.conSiteProvince.findAll(
                ConSiteProvinceSpecification.findJoin(params), PageableSteward.extract(params)
        );
        return conSiteProvincePage;
    }

    @Override
    public ConSiteProvince modify(ConSiteProvince conSiteProvinceNew) {
        return Repositorys.conSiteProvince.saveAndFlush(ConSiteProvinceFactory.initBaseFields(conSiteProvinceNew));
    }

    @Override
    public Optional<ConSiteProvince> single(UUID uuid) {
        return Repositorys.conSiteProvince.findById(uuid);
    }

    @Override
    public List<ConSiteProvince> findAllByUuids(Iterable<UUID> uuids) {
        return Repositorys.conSiteProvince.findAllById(uuids);
    }

    @Override
    public List<ConSiteProvince> delete(List<ConSiteProvince> conSiteProvinceList) {
        conSiteProvinceList.stream().forEach(item -> {
            item.setValidFlag(ValidFlag.DEL);
            ConSiteProvinceFactory.initBaseFields(item);
        });
        return Services.conSiteProvinceTransactional.batchModify(conSiteProvinceList);
    }

}
